package com.fjb.controller.feedback;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonResult;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.feedback.SysFeedbackInfo;
import com.fjb.service.feedback.SysFeedbackInfoService;

import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 意见反馈信息表 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2020-02-13
 */
@Controller
@RequestMapping("/sysFeedbackInfo")
public class SysFeedbackInfoController extends BaseController{
	
	@Autowired
	private SysFeedbackInfoService sysFeedbackInfoService;
	
	/**
	 * @Description:添加反馈
	 * @param request
	 * @param typeId
	 * @return
	 * JsonResult<Boolean>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月13日 下午8:48:36
	 */
	@RequestMapping(value="/addInfo")
	@ResponseBody
	public JsonResult<Boolean> addInfo(HttpServletRequest request,Integer typeId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(typeId==null) {	
			httpCode.setMsg("反馈类型不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String feedbackPhone = request.getParameter("feedbackPhone");
		if(StringUtils.isBlank(feedbackPhone)) {
			httpCode.setMsg("反馈手机号不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String feedbackContent = request.getParameter("feedbackContent");
		if(StringUtils.isBlank(feedbackContent)) {
			httpCode.setMsg("反馈内容不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}
		String feedbackFiles = request.getParameter("feedbackFiles");
		if(StringUtils.isBlank(feedbackFiles)) {
			httpCode.setMsg("反馈文件不能为空");
			return new JsonResult<Boolean>(null, httpCode);
		}	
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		LocalDateTime localDateTime = LocalDateTime.now();
		QueryWrapper<SysFeedbackInfo> queryWrapper = new QueryWrapper<SysFeedbackInfo>();
		queryWrapper.eq("user_id", userId);
		queryWrapper.eq("create_time", localDateTime.toString());
		List<SysFeedbackInfo> list = sysFeedbackInfoService.list(queryWrapper);
		if(list.size()==3) {
			httpCode.setMsg("该用户当天反馈3次已满，请明天赶早");
			return new JsonResult<Boolean>(null, httpCode);
		}
		SysFeedbackInfo fi = new SysFeedbackInfo();
		fi.setUserId(userId);
		fi.setMainUserId(mainUserId);
		fi.setFeedbackTypeId(typeId);
		//fi.setFeedbackName(feedbackName);
		fi.setFeedbackPhone(feedbackPhone);
		fi.setFeedbackContent(feedbackContent);
		fi.setFeedbackFiles(feedbackFiles);
		fi.setFeedbackStatus(1);
		fi.setReviewStatus(2);
		fi.setCreateTime(localDateTime);
		fi.setCreateUserId(userId);
		boolean b = sysFeedbackInfoService.save(fi);
		if(b==false) {
			httpCode.setMsg("添加失败,请联系管理员");
			return new JsonResult<Boolean>(null, httpCode);
		}
		return new JsonResult<Boolean>(null, HttpCode.SUCCESS);
	}
	
}
